package com.sangfor.ssl.service.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes2.dex */
public final class AndroidDnsServer {
    private static final String TAG = "AndroidDnsServer";

    public List<InetAddress> getDnsServer(Context context) {
        ArrayList arrayList = new ArrayList();
        if (Build.VERSION.SDK_INT >= 21) {
            try {
                Log.info(TAG, "Build.VERSION.SDK_INT =" + Build.VERSION.SDK_INT);
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
                Network[] allNetworks = connectivityManager == null ? null : connectivityManager.getAllNetworks();
                if (allNetworks == null) {
                    return arrayList;
                }
                for (Network network : allNetworks) {
                    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
                    String typeName = networkInfo.getTypeName();
                    if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED) && !"VPN".equalsIgnoreCase(typeName)) {
                        Log.info(TAG, "networkInfo.getTypeName=" + typeName);
                        LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
                        if (linkProperties != null) {
                            arrayList.addAll(linkProperties.getDnsServers());
                        }
                    }
                }
                if (arrayList.size() > 0) {
                    return arrayList;
                }
            } catch (Exception e) {
                Log.error(TAG, "Exception in getDnsServer", e);
            }
        }
        List<InetAddress> byReflection = getByReflection();
        if (byReflection != null && byReflection.size() > 0) {
            Log.info(TAG, "getByReflection");
            arrayList.addAll(byReflection);
            return arrayList;
        }
        List<InetAddress> byCommand = getByCommand();
        if (byCommand != null && byCommand.size() > 0) {
            Log.info(TAG, "getByCommand");
            arrayList.addAll(byCommand);
        }
        return arrayList;
    }

    public static List<InetAddress> getByCommand() {
        try {
            LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(Runtime.getRuntime().exec("getprop").getInputStream()));
            ArrayList arrayList = new ArrayList(5);
            while (true) {
                String readLine = lineNumberReader.readLine();
                if (readLine == null) {
                    break;
                }
                int indexOf = readLine.indexOf("]: [");
                if (indexOf != -1) {
                    String substring = readLine.substring(1, indexOf);
                    String substring2 = readLine.substring(indexOf + 4, readLine.length() - 1);
                    if (substring.endsWith(".dns") || substring.endsWith(".dns1") || substring.endsWith(".dns2") || substring.endsWith(".dns3") || substring.endsWith(".dns4")) {
                        InetAddress byName = InetAddress.getByName(substring2);
                        if (byName != null && !TextUtils.isEmpty(byName.getHostAddress()) && !arrayList.contains(byName)) {
                            arrayList.add(byName);
                        }
                    }
                }
            }
            if (arrayList.size() > 0) {
                return arrayList;
            }
            return null;
        } catch (IOException e) {
            Log.error(TAG, "Exception in findDNSByExec", e);
            return null;
        }
    }

    public static List<InetAddress> getByReflection() {
        Method method;
        InetAddress byName;
        try {
            method = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
        } catch (Exception e) {
            Log.error(TAG, "Exception in findDNSByReflection", e);
        }
        if (method == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        String[] strArr = {"net.dns1", "net.dns2", "net.dns3", "net.dns4"};
        for (int i = 0; i < 4; i++) {
            String str = (String) method.invoke(null, strArr[i]);
            if (!TextUtils.isEmpty(str) && (byName = InetAddress.getByName(str)) != null && !TextUtils.isEmpty(byName.getHostAddress()) && !arrayList.contains(byName)) {
                arrayList.add(byName);
            }
        }
        if (arrayList.size() > 0) {
            return arrayList;
        }
        return null;
    }
}
